<?php
namespace App\Service\Xcx;

class YqhService extends BaseService
{
    //邀请函

    //封面
    public static function one($id)
    {
        $db = self::dbConn(self::$dbYqh)->where('del', 0);
        if ($id) {
            $db = $db->where('id', $id);
        } else {
            $db = $db->orderBy('id', 'asc');
        }
        $res = $db->first();
        if (!$res) {
            return self::toback(-2, '没有数据');
        }
        $res->bg = file_exists($res->bg) ? env('DOMAIN').'/'.$res->bg : '';
        return self::toback(0, '', $res);
    }

    //流程列表
    public static function flowList($id, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYqhFlow)
            ->where('del', 0)
            ->where('yqh_id', $id);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(0, [], $total);
        }
        foreach ($res as &$v) {
            $v->detail = mb_strlen($v->cnt)>10 ? mb_substr($v->cnt, 0, 10).'...' : $v->cnt;
            $v->starttime = $v->starttime ? $v->starttime : '-';
            $v->endtime = $v->endtime ? $v->endtime : '-';
        }
        return self::toback(0, '', $res, $total);
    }

    //预约
    public static function apply($id, $uid)
    {
        $res = self::dbConn(self::$dbYqhUid)
            ->where('yqh_id', $id)
            ->where('uid', $uid)
            ->first();
        if ($res) {
            return self::toback(-2, '已预约');
        }
        $res = self::dbConn(self::$dbYqhUid)->insertGetId([
            'yqh_id' => $id, 'uid' => $uid, 'addtime' => toDate(),
        ]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}